package com.xyy.saas.payment.merchant.core.dto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xyy.saas.payment.util.AmountCastUtil;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import java.util.List;

@Data
@Builder
public class CommonRechargeRefundDto {
    private String separateNo;
    private String businessOrderNo;
    private String bizNo;
    /**
     * 业务线
     */
    private String businessIdType;
    /**
     * 支付平台商户标识
     */
    private String accountId;
    /**
     * 支付渠道
     * RechargePayDto#PAY_CHANNEL_WX
     * RechargePayDto#PAY_CHANNEL_ALIPAY
     */
    private String channel;
    /**
     * 原总支付单号
     */
    private String origChannelTradeNo;
    /**
     * 交易金额
     */
    private Integer tradeAmount;
    /**
     * 退款订单明细
     */
    private List<CommonRechargeRefundOrderItem> orderItems;

    /**
     * 组装参数
     */
    public JSONArray assembleOrderItems() {
        if(CollectionUtils.isEmpty(orderItems)) {
            return null;
        }

        JSONArray result = new JSONArray();

        for(CommonRechargeRefundOrderItem item : orderItems) {
            JSONObject obj = new JSONObject();
            obj.put("SubOrderRefundSubAcctNo", item.getThirdUserId());
            obj.put("SubOrderRefundMemberCd", accountId);
            obj.put("SubOrderMemberRefundAmt", AmountCastUtil.castFen2Yuan(item.getAmount()));
            obj.put("SubOrderFeeRefundAmt", NumberUtils.INTEGER_ZERO);
            obj.put("SubOrderRefundOrderNo", item.getRefundNo());
            result.add(obj);
        }

        return result;
    }

    /**
     * 构建明细
     */
    public void buildDetail(String refundNo, String thirdUserId, Integer amount) {
        setOrderItems(Lists.newArrayList(CommonRechargeRefundOrderItem.builder().
                refundNo(refundNo).
                thirdUserId(thirdUserId).
                amount(amount).
                build()));
    }
}

/**
 * 充值明细
 */
@Data
@Builder
class CommonRechargeRefundOrderItem {
    /**
     * 退款订单号
     */
    private String refundNo;
    /**
     * 原充值账户
     */
    private String thirdUserId;
    /**
     * 原充值金额
     */
    private Integer amount;
    /**
     * 备注
     */
    private String remark;
}